<template>
    <div class="header">
        <!-- 折叠按钮 -->
        <div class="collapse-btn" @click="collapseChage">
            <i v-if="!collapse" class="el-icon-s-fold"></i>
            <i v-else class="el-icon-s-unfold"></i>
        </div>
        <div class="logo">{{title}}</div>
        <div class="header-right">
            <div class="header-user-con">
                <!-- 全屏显示 -->
                <div v-if="!isOwner" class="btn-fullscreen" @click="handleFullScreen">
                    <el-tooltip effect="dark" :content="fullscreen?`取消全屏`:`全屏`" placement="bottom">
                        <i class="el-icon-rank"></i>
                    </el-tooltip>
                </div>
                <!-- 最小化 -->
                <div v-if="isOwner" class="btn-other" @click="handleMin">
                    <el-tooltip effect="dark" content="最小化" placement="bottom">
                        <i class="el-icon-minus"></i>
                    </el-tooltip>
                </div>
                <!-- 关闭 -->
                <div v-if="isOwner" class="btn-other" @click="handleClose">
                    <el-tooltip effect="dark" content="关闭" placement="bottom">
                        <i class="el-icon-close"></i>
                    </el-tooltip>
                </div>
            </div>
        </div>
    </div>
</template>
<script>
import bus from '../common/bus';
export default {
    data() {
        return {
            title: config.sSiteTitle,
            collapse: config.sCollapse,
            fullscreen: false,
            name: '...',
            message: 2
        };
    },
    computed: {
        username() {
            let username = localStorage.getItem('ms_username');
            return username ? username : this.name;
        },
        isOwner() {
            return typeof(OWNER) != 'undefined';
        }
    },
    created() {
        bus.$on('titlechange', msg => {
            console.log('titlechange.');
            this.title = msg;
        });
    },
    methods: {
        // 用户名下拉菜单选择事件
        handleCommand(command) {
            if (command == 'loginout') {
                localStorage.removeItem('ms_username');
                this.$router.push('/login');
            }
        },
        // 侧边栏折叠
        collapseChage() {
            this.collapse = !this.collapse;
            bus.$emit('collapse', this.collapse);
        },
        // 关闭事件
        handleClose() {
            if (OWNER) {
                this.$confirm('确定要关闭吗?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    OWNER.doAction(1, "close");
                }).catch(() => {});
            }
        },
        // 最小化
        handleMin() {
            if (OWNER) OWNER.doAction(2, "min");
        },
        // 全屏事件
        handleFullScreen() {
            let element = document.documentElement;
            if (this.fullscreen) {
                if (document.exitFullscreen) {
                    document.exitFullscreen();
                } else if (document.webkitCancelFullScreen) {
                    document.webkitCancelFullScreen();
                } else if (document.mozCancelFullScreen) {
                    document.mozCancelFullScreen();
                } else if (document.msExitFullscreen) {
                    document.msExitFullscreen();
                }
            } else {
                if (element.requestFullscreen) {
                    element.requestFullscreen();
                } else if (element.webkitRequestFullScreen) {
                    element.webkitRequestFullScreen();
                } else if (element.mozRequestFullScreen) {
                    element.mozRequestFullScreen();
                } else if (element.msRequestFullscreen) {
                    // IE11
                    element.msRequestFullscreen();
                }
            }
            this.fullscreen = !this.fullscreen;
        }
    },
    mounted() {
        this.title = config.sSiteTitle;
        if (document.body.clientWidth < 1500) {
            this.collapseChage();
        }
    }
};
</script>
<style scoped>
.mr20 {margin-right: 30px}
.header {
    position: relative;
    box-sizing: border-box;
    width: 100%;
    height: 42px;
    font-size: 22px;
    color: #fff;
}
.collapse-btn {
    float: left;
    padding: 0 12px;
    cursor: pointer;
    line-height: 42px;
}
.header .logo {
    float: left;
    width: 250px;
    line-height: 42px;
}
.header-right {
    float: right;
    padding-right: 8px;
}
.header-user-con {
    display: flex;
    height: 42px;
    align-items: center;
}
.btn-fullscreen {
    transform: rotate(45deg);
    margin-right: 5px;
    font-size: 24px;
}
.btn-other {
    margin-right: 20px;
    font-size: 22px;
    cursor: pointer;
}
.btn-bell,
.btn-fullscreen {
    position: relative;
    width: 30px;
    height: 30px;
    text-align: center;
    border-radius: 15px;
    cursor: pointer;
}
.btn-bell-badge {
    position: absolute;
    right: 0;
    top: -2px;
    width: 8px;
    height: 8px;
    border-radius: 4px;
    background: #f56c6c;
    color: #fff;
}
.btn-bell .el-icon-bell {
    color: #fff;
}
.user-name {
    margin-left: 10px;
}
.user-avator {
    margin-left: 20px;
}
.user-avator img {
    display: block;
    width: 40px;
    height: 40px;
    border-radius: 50%;
}
.el-dropdown-link {
    color: #fff;
    cursor: pointer;
}
.el-dropdown-menu__item {
    text-align: center;
}
</style>
